var express = require('express'),
  router = express.Router(),
  axios = require('axios')

//* 1.酷狗接口
//? 酷狗搜索
router.get('/kugou/search', (req, res) => {
  const path = `http://songsearch.kugou.com/song_search_v2?keyword=${req.query.keyword}&page=${req.query.page}&pagesize=${req.query.pagesize}&platform=WebFilter`
  var config = {
    method: 'get',
    url: encodeURI(path), // 中文转编码
  }

  axios(config)
    .then(function (response) {
      // console.log(JSON.parse(response.data));
      res.send(response.data.data.lists
        .filter(v => {
          // 过滤掉文件hash值和专辑id不全的结果
          return v.FileHash.length && v.AlbumID.length
        }) // 过滤掉试听的
        .filter(v => !v.trans_param.musicpack_advance)
      )
    })
    .catch(function (error) {
      console.log(error)
    })
})

//? 酷狗获取歌曲信息：图片，歌手名，歌曲名，歌词，歌曲链接，是否是试听版
router.get('/kugou/musicInfo', (req, res) => {
  const path = `https://wwwapi.kugou.com/yy/index.php?r=play/getdata&hash=${req.query.hash}&mid=a7b58345c5c37b811e919c10aa9fed96&album_id=${req.query.id}`
  var config = {
    method: 'get',
    url: encodeURI(path), // 中文转编码
  }

  axios(config)
    .then(function (response) {
      // console.log(JSON.parse(response.data));
      res.send(response.data.data)
    })
    .catch(function (error) {
      console.log(error);
    })
})


//* 2.咪咕接口
//? 咪咕搜索
router.get('/migu/search', (req, res) => {
  const path = `http://pd.musicapp.migu.cn/MIGUM3.0/v1.0/content/search_all.do?text=${req.query.keyword}&pageNo=${req.query.page}&pageSize=${req.query.pagesize}&searchSwitch={"song":1,"album":0,"singer":0,"tagSong":0,"mvSong":0,"songlist":0,"bestShow":0}`
  var config = {
    method: 'get',
    url: encodeURI(path), // 中文转编码
  }

  axios(config)
    .then(function (response) {
      // console.log(JSON.parse(response.data))
      // res.send(JSON.parse(response.data).data.songResultData.result)
      res.send(response.data.songResultData.result)
    })
    .catch(function (error) {
      console.log(error)
    })
})

//? 咪咕根据copyrightId获取歌词
router.get('/migu/getLyrics', (req, res) => {
  const path = `http://music.migu.cn/v3/api/music/audioPlayer/getLyric?copyrightId=${req.query.copyrightId}`
  var config = {
    method: 'get',
    url: encodeURI(path),
    headers: {
      'Referer': 'https://music.migu.cn',
    }
  }
  axios(config)
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      console.log(err)
    })
})

//* 3.QQ接口
//? QQ搜索
router.get('/qq/search', (req, res) => {
  // console.log(req.query)
  let path = `http://api88.net/api/qqmusic/?key=06763c2a6def5f30&cache=1&type=so&id=${req.query.keyword}&page=${req.query.page}&nu=${req.query.pagesize}`,
    path2 = `https://api.zsfmyz.top/music/list?p=${req.query.page}&n=${req.query.pagesize}&w=${req.query.keyword}`,
    path3 = `https://c.y.qq.com/soso/fcgi-bin/client_search_cp?aggr=1&cr=1&flag_qc=0&p=${req.query.page}&n=${req.query.pagesize}&w=${req.query.keyword}`
  var config = {
    method: 'get',
    url: encodeURI(path3),
  }
  axios(config)
    .then(response => {
      let {
        data
      } = response
      res.send(JSON.parse(data.slice(0, data.length - 1).replace(/callback\(/, '')).data.song.list.filter(v => !v.pay.payplay))
    })
    .catch(err => {
      console.log(err.message)
    })
})

//? QQ获取歌曲基本信息
router.get('/qq/getMusicInfo', (req, res) => {
  const path = `http://api88.net/api/qqmusic/?key=06763c2a6def5f30&cache=0&type=song&id=${req.query.mid}`
  axios({
      method: 'get',
      url: path,
      headers: {
        'Cookie': 'VGOTCN_OnLineCount=U22'
      }
    })
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      console.log(err)
    })
})


//* 网易云接口
//? 网易云搜索
router.get('/netease/search', (req, res) => {
  // const path = `https://v1.alapi.cn/api/music/search?keyword=${req.query.keyword}&limit=${req.query.pagesize}&offset=${req.query.page}`
  const path = `http://music.eleuu.com/search?keywords=${req.query.keyword}&limit=${req.query.pagesize}&offset=${req.query.page}&type=1`
  axios({
      method: 'get',
      url: encodeURI(path),
      headers: {
        'Cookie': 'NMTID=00OIcaaOOjxEsqZfkyEoM0hPRBPGVMAAAF3cZ4eUQ'
      }
    })
    .then(response => {
      res.send(response.data.result.songs.filter(v => v.fee !== 1))
    })
    .catch(err => {
      console.log(err)
    })
})

//? 网易云获取封面
router.get('/netease/getAlbumImg', (req, res) => {
  const path = `http://music.eleuu.com/album?id=${req.query.id}`
  axios({
      method: 'get',
      url: path,
      headers: {
        'Cookie': 'NMTID=00OIcaaOOjxEsqZfkyEoM0hPRBPGVMAAAF3cZ4eUQ'
      }
    })
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      console.log(err)
    })
})

//? 网易云获取歌词
router.get('/netease/getLyrics', (req, res) => {
  const path = `http://music.eleuu.com/lyric?id=${req.query.id}`
  axios({
      method: 'get',
      url: path,
    })
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      console.log(err)
    })
})

//? 网易云检查歌曲是否可用
router.get('/netease/check', (req, res) => {
  const path = `http://music.eleuu.com/check/music?id=${req.query.id}`
  axios({
      method: 'get',
      url: path,
    })
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      console.log(err)
    })
})

//? 网易获取歌曲链接
router.get('/netease/getMusicUrl', (req, res) => {
  const path = `http://music.eleuu.com/song/url?id=${req.query.id}`
  axios({
      method: 'get',
      url: path,
    })
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      console.log(err)
    })
})

//* 酷我接口
//? 酷我搜索
router.get('/kuwo/search', (req, res) => {
  const path = `http://m.kuwo.cn/newh5app/api/mobile/v1/search/all?key=${req.query.keyword}&pn=${req.query.page}&rn=${req.query.pagesize}`
  axios({
      method: 'get',
      url: encodeURI(path)
    })
    .then(response => {
      res.send(response.data.data.music.filter(v => v.pay_info.play !== "1111"))
    })
    .catch(err => {
      console.log(err)
    })
})

//? 酷我获取歌曲信息
router.get('/kuwo/getMusicInfo', (req, res) => {
  const path = `https://www.kuwo.cn/api/www/music/musicInfo?mid=${req.query.id}`
  axios({
      method: 'get',
      url: path,
      headers: {
        'Cookie': 'kw_token=S; kw_token=ED6HR3LIAEI',
        'csrf': 'S'
      }
    })
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      console.log(err)
    })
})

//? 酷我获取歌曲链接
router.get('/kuwo/getMusicUrl', (req, res) => {
  const path = `http://api.63code.com/kuwo/api.php?id=${req.query.id}&type=json`
  axios({
      method: 'get',
      url: path
    })
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      console.log(err)
    })
})

//* 检查音乐链接是否可用
// router.post('/checkURL', (req, res) => {
//   console.log(req.body)
//   res.send('post')
//   // var isSuccess = false
//   // axios({
//   //     method: 'get',
//   //     url: req.query.url
//   //   })
//   //   .then(response => {
//   //     if (response.status === 200) {
//   //       isSuccess = true
//   //     }
//   //     res.send({
//   //       success: isSuccess,
//   //       code: 1
//   //     })
//   //     // console.log(response)
//   //   })
//   //   .catch(err => {
//   //     // res.send({
//   //     //   success: isSuccess,
//   //     //   code: 0
//   //     // })
//   //     console.log(err)
//   //   })
// })

//* 获取用户当前IP
router.get('/getIP', (req, res) => {
  axios({
      method: 'get',
      url: 'http://pv.sohu.com/cityjson?ie=utf-8'
    })
    .then(response => {
      res.send(response.data)
    })
    .catch(err => {
      res.send('获取浏览器IP失败' + err)
    })
})

router.post('/submit', (req, res) => {
  res.send(`POST http://127.0.0.1:8000/submit`);
})

module.exports = router
